package com.gt.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.security.NoSuchAlgorithmException;

public class PosThreeDESUtil {
	private static final String hexString = "0123456789ABCDEF";

	public static String key = "d@9dfkjkj#112AADFF";//秘钥
	  
	  private static byte[] hex(String key)
	  {
	    String f = DigestUtils.md5Hex(key);
	    byte[] bkeys = new String(f).getBytes();
	    byte[] enk = new byte[24];
	    for (int i = 0; i < 24; i++) {
	      enk[i] = bkeys[i];
	    }
	    return enk;
	  }
	  
	  public static String PosEncode3Des(String key, String srcStr)
	  {
	    byte[] keybyte = hex(key);
	    byte[] src = srcStr.getBytes();
	    try
	    {
	      SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
	      
	      Cipher c1 = Cipher.getInstance("DESede");
	      c1.init(1, deskey);
	      String pwd = Base64.encodeBase64String(c1.doFinal(src));
	      
	      return PosStrToHEX(pwd);
	    }
	    catch (NoSuchAlgorithmException e1)
	    {
	      e1.printStackTrace();
	    }
	    catch (NoSuchPaddingException e2)
	    {
	      e2.printStackTrace();
	    }
	    catch (Exception e3)
	    {
	      e3.printStackTrace();
	    }
	    return null;
	  }
	  
	  public static String PosDecode3Des(String key, String desStr)
	  {
	    Base64 base64 = new Base64();
	    byte[] keybyte = hex(key);
	    desStr = PosHEXToSTR(desStr);
	    byte[] src = base64.decode(desStr);
	    try
	    {
	      SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
	      
	      Cipher c1 = Cipher.getInstance("DESede");
	      c1.init(2, deskey);
	      return new String(c1.doFinal(src));
	    }
	    catch (NoSuchAlgorithmException e1)
	    {
	      e1.printStackTrace();
	    }
	    catch (NoSuchPaddingException e2)
	    {
	      e2.printStackTrace();
	    }
	    catch (Exception e3)
	    {
	      e3.printStackTrace();
	    }
	    return null;
	  }
	  
	  public static String PosStrToHEX(String str)
	  {
	    byte[] bytes = (byte[])null;
	    try
	    {
	      bytes = str.getBytes("UTF-8");
	    }
	    catch (Exception e)
	    {
	      e.printStackTrace();
	    }
	    StringBuilder sb = new StringBuilder(bytes.length * 2);
	    for (int i = 0; i < bytes.length; i++)
	    {
	      sb.append("0123456789ABCDEF".charAt((bytes[i] & 0xF0) >> 4));
	      sb.append("0123456789ABCDEF".charAt((bytes[i] & 0xF) >> 0));
	    }
	    return sb.toString();
	  }
	  
	  public static String PosHEXToSTR(String bytes)
	  {
	    String sRes = "";
	    ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
	    for (int i = 0; i < bytes.length(); i += 2) {
	      baos.write("0123456789ABCDEF".indexOf(bytes.charAt(i)) << 4 | "0123456789ABCDEF".indexOf(bytes.charAt(i + 1)));
	    }
	    try
	    {
	      sRes = new String(baos.toByteArray(), "UTF-8");
	    }
	    catch (Exception e)
	    {
	      e.printStackTrace();
	    }
	    return sRes;
	  }
	  public static void main(String args[]) {
		String aa =   PosEncode3Des("d@9dfkjkj#112AADFF","123");
		System.out.println(aa);
		
		String bb = PosDecode3Des("d@9dfkjkj#112AADFF",aa);
		System.out.println(bb);
	  }
}
